package com.voicechat.firstmet.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.MainActivity;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.LoginService;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.TimerUtil;
import com.voicechat.firstmet.view.SecurityCodeView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity_imessage
 * @ClassName: GetCodeActivity
 * @Description: 验证码登录
 * @Author: ljp
 * @CreateDate: 2018/8/30 16:54
 * @Version: 1.0
 */
public class GetCodeActivity extends BaseActivity {
    @BindView(R.id.sc_code)
    SecurityCodeView scCode;
    @BindView(R.id.login_sure)
    TextView loginSure;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    TimerUtil mTimer;
    @BindView(R.id.iv_back)
    LinearLayout ivBack;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    private String TAG = "GetCodeActivity";
    //flag 0注册，1登录，2忘记密码
    private String flag;
    private String mobile;
    private int code;

    @Override
    protected int setStatusBarColor() {
        return R.color.colorWhite;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_login_code;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        mobile = getIntent().getStringExtra("mobile");
        flag = getIntent().getStringExtra("flag");
        code = getIntent().getIntExtra("code",-1);
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        initView();
    }
    private void initView() {
        tvPhone.setText("已经发送验证码至"+mobile);
        loginSure.setClickable(false);
        mTimer = new TimerUtil(60, tvGetCode, this);
        if(flag.equals("0")) {
            if (code == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                mTimer.myStart(0);
                toast_MakeText("验证码发送成功!");
            }else {
                toast_MakeText("验证码发送失败!");
            }
        }else {
            getVaCode();
        }
        scCode.setInputCompleteListener(new SecurityCodeView.InputCompleteListener() {
            @Override
            public void inputComplete() {
                String trim = scCode.getEditContent().trim();
                if (trim.length() == 4) {
                    loginSure.setClickable(true);
                    loginSure.setBackgroundResource(R.drawable.login_btn);
                }
            }
            @Override
            public void deleteContent(boolean isDelete) {
                loginSure.setBackgroundResource(R.drawable.login_btn_2);
            }
        });
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    @OnClick({R.id.login_sure, R.id.tv_get_code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_sure:
                codeHttp();
                break;
            //重新获取验证码
            case R.id.tv_get_code:
                getVaCode();
                break;
        }
    }
    /**
     * @return
     * @method
     * @description 获取验证码
     * @date: 2018/9/19 11:30
     * @author: ljp
     */
    private void getVaCode() {
        //flag 判断 0注册，1登录，2忘记密码
        String type = null;
        int code = 0;
        switch (flag) {
            case "0":
                type = LoginService.VALIDATE_CODE_TYPE.VERIFY_CODE_REGISTER;
                code = HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS;
                break;
            case "1":
                type = LoginService.VALIDATE_CODE_TYPE.VALIDATE_CODE_LOGIN;
                code = HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS;
                break;
            case "2":
                type = LoginService.VALIDATE_CODE_TYPE.VALIDATE_CODE_CHANGE;
                code = HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS;
                break;
        }
        int finalCode = code;
        LoginService.loginGetVCode(mobile, type).subscribe(result -> {
            Log.i(TAG, "getVaCode: ----"+result.toString());
            if(result.getCode() == finalCode){
                mTimer.myStart(0);
                toast_MakeText(result.getMsg());
            }else {
                toast_MakeText(result.getMsg());
            }
        }, throwable -> {
            Log.i(TAG, "getVaCode: -----"+throwable.toString());

        });
    }
    /**
     * @return
     * @method
     * @description 网络请求 校验验证码
     * @date: 2018/9/19 11:28
     * @author: ljp
     */
    private void codeHttp() {
        //flag 判断 0注册，1登录，2修改密码
        String trim = scCode.getEditContent().trim();
       /* String type = null;
        switch (flag) {
            case "0":
                type = LoginService.VALIDATE_CODE_TYPE.VERIFY_CODE_REGISTER;
                break;
            case "1":
               initLogin(trim);
                break;
            case "2":
                type = LoginService.VALIDATE_CODE_TYPE.VALIDATE_CODE_CHANGE;
                break;
        }*/
       if(flag.equals("1")){
           initLogin(trim);
       }else {
           LoginService.verificationCode(mobile, trim).subscribe(result -> {
               Log.i(TAG, "codeHttp: ------"+result.toString());
               //返回成功
               if (result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS){
                   toast_MakeText(result.getMsg());
                   Intent intent = null;
                   //通过flag判断跳转到哪个页面
                   switch (flag) {
                       case "0":
                           intent = new Intent(this,SetPassWordActivity.class);
                           intent.putExtra("mobile",mobile);
                           intent.putExtra("flag","0");
                           startActivity(intent);
                           break;
                       case "2":
                           intent = new Intent(this,SetPassWordActivity.class);
                           intent.putExtra("flag","1");
                           intent.putExtra("mobile",mobile);
                           startActivity(intent);
                           break;
                   }
               }else {
                   toast_MakeText(result.getMsg());
               }
               Log.i(TAG, "codeHttp: -----" + result.toString());
           }, throwable -> {
               Log.i(TAG, "codeHttp: ----" + throwable.toString());
           });
       }
    }
    /**
    * @method
    * @description 登录校验验证码
    * @date: 2018/9/25 20:08
    * @author: ljp
    * @return
    */
    private void initLogin(String code) {
        String deviceInfo = PhoneUtils.getDeviceInfo(context);
        LoginService.verificationCodeLogin(mobile,code,deviceInfo).subscribe(mySelfInfoBean -> {
            Log.i(TAG, "codeHttp: ------"+mySelfInfoBean.toString());
            //返回成功
            if (mySelfInfoBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS){
                MySelfInfoBean.ObjectBean object = mySelfInfoBean.getObject();
                SPUtils.setEditorKeyValue(SPUtils.SP_PHONE,mobile);
                SPUtils.setEditorKeyValue(SPUtils.SP_TOKEN,object.getToken());
                SPUtils.setEditorKeyValue(SPUtils.SP_USERSIG,mySelfInfoBean.getMap().getUserSig());
                SPUtils.setEditorKeyValue(SPUtils.SP_MYSELFINFO, ParcelableUtils.marshall(object));
               // LoginHelper loginHelper = new LoginHelper(this);
                //loginHelper.login(object.getId(),mySelfInfoBean.getMap().getUserSig());
                toast_MakeText(mySelfInfoBean.getMsg());
                Intent intent = null;
                //通过flag判断跳转到哪个页面
                removeAllActivity();
                intent = new Intent(this,MainActivity.class);
                startActivity(intent);
            }else if(mySelfInfoBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_INFODATA){//用户未完善资料
                MySelfInfoBean.ObjectBean object = mySelfInfoBean.getObject();
                SPUtils.setEditorKeyValue(SPUtils.SP_PHONE,mobile);
                SPUtils.setEditorKeyValue(SPUtils.SP_TOKEN,object.getToken());
                SPUtils.setEditorKeyValue(SPUtils.SP_USERSIG,mySelfInfoBean.getMap().getUserSig());
                SPUtils.setEditorKeyValue(SPUtils.SP_MYSELFINFO, ParcelableUtils.marshall(object));
                Intent intent = new Intent(this, RegisterActivity.class);
                intent.putExtra("mobile",mobile);
                startActivity(intent);
                removeActivity();
            }else {
                toast_MakeText(mySelfInfoBean.getMsg());
            }
            Log.i(TAG, "codeHttp: -----" + mySelfInfoBean.toString());
        }, throwable -> {
            Log.i(TAG, "codeHttp: ----" + throwable.toString());
        });
    }


}
